function blogHub() {
	
}

blogHub.prototype.settings = {
	blogsPerList:6,
	blogsPerTab:6,
	queryOptions:""
}

blogHub.prototype.init = function(targetDiv) {
	//Controller
	if (typeof(blogHubController)!="object") {
		blogHubController = {
			targetDiv:targetDiv,
			settings:this.settings,
			init:function() {
				// seed database with list of sites/data
				blogHubView.init();
				blogHubDB.addSitesToLibrary();
				// call listView from View object
			},
			scrollItems:function() {
				//get set of items from DB, feed to displayScrolItems in View
			},
			launchMoreBox:function() {
				if ($("#blogAllView-holder").length<1) {
					blogHubView.allView.init(blogHubDB.currentIndex);	
				}
				blogHubView.allView.launchTB();
			}
		}		
	}
	
	//View
	if (typeof(blogHubView)!="object") {
		blogHubView = {
			init:function() {
				$("#"+blogHubController.targetDiv).html('<div id="blogList"></div>');
			},
			listView:function() {
				if (blogHubDB.library.length>0) {
					for (var i=0; (i<blogHubDB.library.length)&&(i<blogHubController.settings.blogsPerList); i++) {
						var self = blogHubDB.library[i];
						$("#blogList").append('<li><a target="_blank" href="'+self.siteLink.$t+'">'+self.siteName.$t+"</a></li>");
					
					}
					
					//Add the more button
					/*if (blogHubDB.library.length > blogHubController.settings.blogsPerList){
						$("#"+blogHubController.targetDiv).append('<div id="blogMore"><a href="javascript:blogHubController.launchMoreBox();">More</a></div>');	
					}*/
					
				} else {
					//console.log("There is no data to display. Spreadsheed returned zero results.");
				}
			},
			allView:{
				init:function (startIndex){
					//Construct div with contents
					if($("#blogAllView-holder").length<1) {
						$("#"+blogHubController.targetDiv).append('<div style="display:none;" id="blogAllView-hook"><div style="height:350px; width:680px; margin:30px auto;" align="center" id="blogAllView-holder"></div><table border="0" cellpadding="10" cellspacing="0" align="center"><tr><td><div id="blogTabLess"></div></td><td><div id="blogTabMore"></div></td></tr></table></div>');
					} else {
						$("#blogAllView-holder").empty();						
					}
					
					//loop through contents
					for (var i=startIndex; (i<blogHubDB.library.length)&&(i<(parseInt(startIndex)+parseInt(blogHubController.settings.blogsPerTab))); i++) {
						this.drawBlogLink(i);
						blogHubDB.currentIndex = i;
					}
					this.createButtons(startIndex);
				},
				drawBlogLink: function(id) {
					var self = blogHubDB.library[id];
					$("#blogAllView-holder").append('<div style="width:220px; height:180px; float:left; overflow:hidden;"><a target="_blank" href="'+self.siteLink.$t+'"><img border="0" src="http://mozshot.nemui.org/shot/140x120?'+self.siteLink.$t+'"/></a><div style="margin-top:12px; margin-left:10px; margin-right:20px;"><a target="_blank" href="'+self.siteLink.$t+'">'+self.siteLink.$t+"</a></div></div>");
				},
				launchTB: function() {
					tb_show("More Blogs and Websites",  "#TB_inline?height=470&width=700&inlineId=blogAllView-hook");	
				},
				createButtons:function(startIndex) {
					//Create More Button
					
					if ((parseInt(startIndex)+parseInt(blogHubController.settings.blogsPerTab))<blogHubDB.library.length) {
						$("#blogTabMore").html('<p><a href="javascript:blogHubView.allView.init('+eval(parseInt(startIndex)+parseInt(blogHubController.settings.blogsPerTab))+');">More</a></p>');
					} else {
						$("#blogTabMore").empty();	
					}
					
					//Create Less Button
					if ((parseInt(startIndex)-parseInt(blogHubController.settings.blogsPerTab))>=0) {
						$("#blogTabLess").html('<p><a href="javascript:blogHubView.allView.init('+eval(parseInt(startIndex)-parseInt(blogHubController.settings.blogsPerTab))+');">Back</a></p>');
					} else {
						$("#blogTabLess").empty();	
					}
				}
			}
			
		}
	}
	
	//Model
	if (typeof(blogHubDB)!="object") {
		blogHubDB = {
			currentIndex:"",
			totalResults:"",
			library: new Array(),
			addSitesToLibrary:function() {
				var url = "http://spreadsheets.google.com/feeds/list/rL1fPxJlRXWgRW1nQElPa5A/1/public/values?alt=json-in-script&reverse=true&sq=approved=1"
				$.ajax({
					type:"GET",
					dataType:"jsonp",
					//libInfo:libInfo,
					url:url,
					error: function(XMLHttpRequest, textStatus, errorThrown){
						$('<p></p>')
						.html("There was an error with your request")
						.appendTo('body');
						returnVal = false;
					},
					success: function(html) {
						var queryData = eval(html);
						blogHubDB.currentIndex = queryData.feed.openSearch$startIndex.$t-1;
						blogHubDB.totalResults = queryData.feed.openSearch$totalResults.$t;
						
						for (var i=0; i < queryData.feed.entry.length; i++) {
							var self = queryData.feed.entry[i];
							d = {
								siteLink: self.gsx$link,
								siteType: self.gsx$sitetype,
								timestamp: self.gsx$timestamp,
								yourName: self.gsx$yourname,
								siteName: self.gsx$blogwebsitetitle,
								approved: self.gsx$approved.$t
							}
							blogHubDB.library.push(d);
						}
						
					},
					complete: function() {
						blogHubView.listView();
					}
				});
			},
			getSet:function() {
				
			}
			
			
		}
	}
	blogHubController.init();
}